package com.clayecloud.module_main.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Bundle;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.lib_base.action.HandlerAction;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_base.adapter.FragmentPagerAdapter;
import com.clayecloud.module_main.R;
import com.clayecloud.module_main.databinding.ActivityMainPageBinding;

import com.clayecloud.module_main.manager.ActivityManager;
import com.clayecloud.module_main.other.DoubleClickHelper;
import com.clayecloud.module_main.ui.fragment.HomeFragment;
import com.clayecloud.module_main.ui.fragment.LifeFragment;
import com.clayecloud.module_main.ui.fragment.MeFragment;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;

@Route(path = "/main/index")
public class MainPageActivity extends AppCompatActivity implements ToastAction, HandlerAction {

    //scan回调校验码
    private final int REQUEST_CODE_SCAN_ONE =112;

    private ActivityMainPageBinding activityMainBinding;
    private FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main_page);
        //初始化页面
        initView();
    }

    @Override
    public void onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            toast("再按一次退出");
            return;
        }
        moveTaskToBack(false);
        //清空所有的页面
        postDelayed(() -> {
            // 进行内存优化，销毁掉所有的界面
            ActivityManager.getInstance().finishAllActivities();
        }, 300);
    }

    public void initView(){
        fragmentPagerAdapter = new FragmentPagerAdapter(this);
        fragmentPagerAdapter.addFragment(new HomeFragment());
        fragmentPagerAdapter.addFragment(new LifeFragment());
        fragmentPagerAdapter.addFragment(new MeFragment());
        activityMainBinding.ViewPager.setAdapter(fragmentPagerAdapter);
        activityMainBinding.bottomBarLayout.setViewPager(activityMainBinding.ViewPager);
    }

    @Override
    protected  void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        if (requestCode == REQUEST_CODE_SCAN_ONE) {
            // 导入图片扫描返回结果
            HmsScan obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj != null) {
                // 展示解码结果
                toast(obj.showResult);
            }
        }
    }
}